package com.javasm.duanju.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.javasm.duanju.dto.VideoUploadDTO;
import com.javasm.duanju.entity.DraftVideo;
import com.javasm.duanju.entity.DraftVideoItem;
import com.javasm.duanju.service.DraftVideoItemService;
import com.javasm.duanju.service.impl.QiNiuService;
import commom.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author Administrator
 * @date 2025-02-26 20:21
 */

@RestController
@RequestMapping("/video")
public class UploadController {
    @Autowired
    private QiNiuService qiNiuService;
      @Autowired
      private DraftVideoItemService draftVideoItemService;
    @Autowired
    private RedisTemplate redisTemplate;


    @PostMapping("/upImage")
    public AjaxResult upImage(MultipartFile img) {
        String filename = img.getOriginalFilename();
        String ext = filename.substring(filename.lastIndexOf("."));
//        上传文件
        String path = "64/zhao/temp/" + UUID.randomUUID().toString() + ext;
        qiNiuService.upload(img, path);
        return AjaxResult.ok(path);
    }

    @PostMapping("/upVideo")
    public AjaxResult upVideo(MultipartFile upVideo) {
        String filename = upVideo.getOriginalFilename();
        String ext = filename.substring(filename.lastIndexOf("."));
//        上传文件
//      TODO:  从redis拿到短剧信息
        DraftVideo draftVideo = (DraftVideo) redisTemplate.opsForValue().get("dj-info");
        LambdaQueryWrapper<DraftVideoItem> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DraftVideoItem::getVideoId,draftVideo.getId());
        List<DraftVideoItem> list = draftVideoItemService.list(lambdaQueryWrapper);
//         拿出每一集id
        for (DraftVideoItem draftVideoItem : list) {
            Long draftVideoItemId = draftVideoItem.getId();
            String path = "dj/video/" +draftVideo.getId()+"/"+draftVideoItemId+"/"  +draftVideoItemId + ext;
            qiNiuService.upload(upVideo, path);
            return AjaxResult.ok(path);
        }
//        生成文件新

        return AjaxResult.ok("");
    }
}
